package com.itheima.reggie.mapper;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

public interface DishMapper {
    @Select("select count(*) from dish where category_id =#{id}")
    Integer findByCid(long cid);

    //添加菜品，并且获取到添加后的主键
    //@Options: 操作配置
    //  useGeneratedKeys = true 获取插入后自增长的的主键值
    //  keyColumn ="id",keyProperty = "id" 将数组主键字段id的值赋值给当前传入对象的id属性赋值
    @Insert("insert into  dish " +
            "values(null,#{name},#{categoryId},#{price},#{code}," +
            "#{image},#{description},#{status},#{sort},#{createTime}," +
            "#{updateTime},#{createUser},#{updateUser},0)")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    Integer save(DishDto dishDto);

    //查询所有菜品数据
    @Select("SELECT d.*,c.name category_name FROM dish d\n" +
            "INNER JOIN category c ON d.category_id = c.id where d.is_deleted=0")
    List<DishDto>findByPage();

    //根据菜品id查询一个菜品和对应多条口味记录数据
    DishDto findById(long id);

    //根据id修改菜品
    void updateById(DishDto dishDto);

    //根据id删除菜品
    @Delete("delete from dish where id=#{id}")
    void deleteById(long id);

    @Select("select * from dish where category_id=#{cid}")
    List<Dish> findAllByCid(@Param("cid") Long cid, @Param("status") Integer status);

    //根据ids查找启用的菜品
    Integer findByIds(@Param("ids") List<Long> ids);

    //批量删除菜品
    void deleteByIds(@Param("ids") List<Long> ids,
                     @Param("opr") Long opr,
                     @Param("updateTime") LocalDateTime updateTime);

    //修改菜品的状态
    void updateStatusFByIds(@Param("ids") List<Long>ids,
                            @Param("opr") long opr,
                            @Param("updateTime") LocalDateTime updateTime);
    //修改菜品的状态
    void updateStatusTByIds(@Param("ids") List<Long>ids,
                            @Param("opr") long opr,
                            @Param("updateTime") LocalDateTime updateTime);

}
